Skip to content

Conversation

@dohy-eon
Copy link
Member

[feat] 다솜소식 API 구현

Issue

변경 내용

  • 다솜소식 조회, 등록 API 구현

구현 사항

  • NewsRequest, ResponseDto
  • Contoller
  • Service
  • Entity
  • Repository

테스트 (필요 시)

  • 저,,, 무슨 문제인진 모르겠지만 redis가 제대로 설치가 안돼서 postman에서 못돌려봤습니다..... Windows에서 지원을 중단하였다고 하는데 해결방법 아시는 분 헬프 부탁드립니다.

@dohy-eon dohy-eon added the FEAT label Feb 14, 2025
@dohy-eon dohy-eon requested review from hodoon and ysw789 February 14, 2025 02:22
@dohy-eon dohy-eon self-assigned this Feb 14, 2025
@dohy-eon dohy-eon linked an issue Feb 14, 2025 that may be closed by this pull request
@Setter
@Entity
@Schema(description = "뉴스 엔티티")
public class NewsEntity {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BaseEntity 상속받아서 생성일, 수정일, 상태 필드 구성하면 될 것 같습니다


public List<NewsResponseDto> getAllNews() {
return newsRepository.findAll().stream()
.map(news -> new NewsResponseDto(news.getId(), news.getTitle(), news.getContent(), news.getCreatedAt(), news.getImageUrl()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

News 엔티티 클래스 내부에 Dto 변환 메소드를 구현하면 더 깔끔하게 구현 가능합니다
Applicant 메소드 참고해보세용

}

public NewsResponseDto createNews(NewsRequestDto requestDto) {
NewsEntity news = new NewsEntity();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setter를 이용해 객체 필드를 초기화하는 것보단 생성자나 Builder 패턴을 이용해서 인스턴스 생성과 동시에 초기화 하는 것이 더 적절합니다.
setter를 쓰게 되면 인스턴스 생성 후 객체 내부 상태를 변경하는 행위가 되는 것인데, 인스턴스 생성 이외의 상태 변경은 별도의 update 메소드 같이 도메인 로직 내에서 명시적으로 허용된 경우에만 이루어져야합니다.

news.setCreatedAt(LocalDateTime.now());

NewsEntity savedNews = newsRepository.save(news);
return new NewsResponseDto(savedNews.getId(), savedNews.getTitle(), savedNews.getContent(), savedNews.getCreatedAt(), savedNews.getImageUrl());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

마찬가지로 Entity -> Dto 변환 메소드를 엔티티 내부에 구현하면 깔끔합니다

private LocalDateTime createdAt;

@Schema(description = "뉴스 이미지 URL", example = "http://example.com/image.jpg")
private String imageUrl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이미지는 외부 DB에 저장하는 건가요?


@Tag(name = "NEWS API", description = "다솜소식 API")
@RestController
@RequestMapping("/news")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엔드포인트는 /api 로 시작하는 걸로 통일할게용
(ex. /api/news)

@Operation(summary = "소식 등록", description = "새로운 소식을 등록")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "생성 완료"),
@ApiResponse(responseCode = "400", description = "유효하지 않은 요청 데이터",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request Body -> NewsRequestDto 역직렬화 과정에서 필드 값 Validation을 하기 위해서는 NewsRequestDto 선언부 앞에 @Valid 어노테이션을 추가하고 NewsRequestDto의 각 필드에 제약 조건을 설정하면 됩니다. ApplicantCreateRequestDto를 참고해보세요


@Schema(description = "소식 ID", example = "1")
private Long id;
private final Long id;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DTO를 불변 객체로 만들 필요는 없으므로 필드에 final 키워드는 빼는 것이 좋아보입니다

import lombok.*;

@Getter
@Setter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setter를 사용하는 곳이 없다면 어노테이션은 제거해주세요

@ysw789
Copy link
Member

ysw789 commented Feb 18, 2025

GOOD 👍

@dohy-eon dohy-eon merged commit 2310796 into dev Feb 21, 2025
1 check passed
@ysw789 ysw789 deleted the feat/#33 branch March 1, 2025 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feat] 다솜소식 API 구현

4 participants